package Package.Example_6;

import java.io.*;
import java.net.Socket;

public class Srver implements Runnable {
    private final Socket s;
    public Srver(Socket s) {
        this.s = s;
    }
    @Override
    public void run() {
        try {
            BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
            int count=0;
            File file=new File("D:\\insert\\copy["+count+"].txt");
            while (file.exists()){
                count++;
                file=new File("D:\\insert\\copy["+count+"].txt");
            }
            BufferedWriter bw=new BufferedWriter(new FileWriter(file));
            String line;
            while ((line=br.readLine())!=null) {
                bw.write(line);
                bw.newLine();
                bw.flush();
            }
            BufferedWriter bwServer = new BufferedWriter(new
                    OutputStreamWriter(s.getOutputStream()));
            bwServer.write("文件上传成功");
            bwServer.newLine();
            bwServer.flush();
//释放资源

            s.close();
            System.out.println("-----------------");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
